package com.ddx.controller;

import com.ddx.common.exception.ExceptionUtil;
import com.ddx.common.exception.ResultModel;
import com.ddx.controller.core.ProvideIdeaController;
import com.ddx.dto.ProvideIdeaDto;
import com.ddx.dto.ShortVideoDto;
import com.ddx.service.ShortVideoService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: liyuxiang
 * @create: 2018-11-05
 */
@RestController
@RequestMapping(value = "/app")
public class ShortVideoController {

    private static Logger logger = LoggerFactory.getLogger(ShortVideoController.class);

    @Autowired
    ShortVideoService shortVideoService;


    @ApiOperation(value = "精彩页面", notes = "")
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/video/marvellous", method = { RequestMethod.POST })
    public ResultModel marvellous(@RequestBody ShortVideoDto shortVideoDto, HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            if(StringUtils.isNotBlank(request.getHeader("userId"))) {
                shortVideoDto.setUserId(Integer.parseInt(request.getHeader("userId")));
            }
            resultModel.setData(shortVideoService.marvellous(shortVideoDto));
        } catch (Exception e) {
            logger.error("精彩页面异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }

    @ApiOperation(value = "关注页面", notes = "")
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/video/follow", method = { RequestMethod.POST })
    public ResultModel follow(@RequestBody ShortVideoDto shortVideoDto, HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            // 获取userId
            if(StringUtils.isNotBlank(request.getHeader("userId"))) {
                shortVideoDto.setUserId(Integer.parseInt(request.getHeader("userId")));
            }
            resultModel.setData(shortVideoService.follow(shortVideoDto));
        } catch (Exception e) {
            logger.error("关注页面异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }

    @ApiOperation(value = "分类页面", notes = "")
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/video/type", method = { RequestMethod.POST })
    public ResultModel type(@RequestBody ShortVideoDto shortVideoDto, HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            if(StringUtils.isNotBlank(request.getHeader("userId"))) {
                shortVideoDto.setUserId(Integer.parseInt(request.getHeader("userId")));
            }
            resultModel.setData(shortVideoService.type(shortVideoDto));
        } catch (Exception e) {
            logger.error("分类页面异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    @ApiOperation(value = "用户发布页面", notes = "")
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/user/works", method = { RequestMethod.POST })
    public ResultModel works(@RequestBody ShortVideoDto shortVideoDto, HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            if(StringUtils.isNotBlank(request.getHeader("userId"))) {
                shortVideoDto.setUserId(Integer.parseInt(request.getHeader("userId")));
            }
            resultModel.setData(shortVideoService.works(shortVideoDto));
        } catch (Exception e) {
            logger.error("用户发布页面异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    
    @ApiOperation(value = "视频详情页面，传视频ID", notes = "")
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/video/detail", method = { RequestMethod.POST })
    public ResultModel detail(@RequestBody ShortVideoDto shortVideoDto, HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            String userId=request.getHeader("userId");
            if(StringUtils.isNotBlank(userId)){
                shortVideoDto.setUserId(Integer.parseInt(userId));
            }
            resultModel.setData(shortVideoService.detail(shortVideoDto));
        } catch (Exception e) {
            logger.error("视频详情页面异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
    @ApiOperation(value = "搜索功能", notes = "")
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/video/search", method = { RequestMethod.POST })
    public ResultModel search(@RequestBody Map<String,Object> map, HttpServletRequest request) {
        ResultModel resultModel = ResultModel.getSuccessInstance();
        try {
            resultModel.setData(shortVideoService.search(map));
        } catch (Exception e) {
            logger.error("搜索功能异常", e);
            resultModel = ExceptionUtil.getMessageException(e);
        }
        return resultModel;
    }
}
